﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace SoftWareBoot
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class Window_StartSetting : Window
    {
        /// <summary>
        /// 升级服务器地址
        /// </summary>
        public string ServiceUri
        {
            get;
            private set;
        }

        /// <summary>
        /// 项目名称
        /// </summary>
        public string ProjectName
        {
            get;
            private set;
        }
        public Window_StartSetting()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 配置取消事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        private void Txt_Url_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (!(e.Key == Key.Enter && Txt_Url.Text.ToLower() != "")) return;

            string uri = Txt_Url.Text;

            ProgBar_Wait.Visibility = System.Windows.Visibility.Visible;

            new System.Threading.Thread(() =>
            {

                App.DoEvents();
                var client = new AutoUpdateService.AutoUpdateClient(App.getBinding(), new System.ServiceModel.EndpointAddress(uri));

                //client.Endpoint.Address = new System.ServiceModel.EndpointAddress(uri);
                try
                {
                    string[] names = client.GetPrjNames();

                    Dispatcher.BeginInvoke(new Action(() =>
                    {

                        Cmb_PrjName.ItemsSource = names;

                    }));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "服务错误", MessageBoxButton.OK, MessageBoxImage.Error);
                }

                ProgBar_Wait.Dispatcher.BeginInvoke(new Action(() =>
                {
                    ProgBar_Wait.Visibility = System.Windows.Visibility.Hidden;
                }));
            }).Start();

        }


        /// <summary>
        /// 配置确认事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Set_Click(object sender, RoutedEventArgs e)
        {
            if (Txt_Url.Text == "")
            {
                MessageBox.Show("请填写自动升级服务器地址...", "提示", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                Txt_Url.Focus();
                return;
            }

            if (Cmb_PrjName.Text == "")
            {
                MessageBox.Show("请选择升级绑定的项目名称...", "提示", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                Cmb_PrjName.Focus();
                return;
            }

            ServiceUri = Txt_Url.Text.Trim();

            ProjectName = Cmb_PrjName.Text;

            DialogResult = true;

            Close();

        }
    }
}
